Homework Assignment 1

 

Due: 02/18/09

 

 

  1. Assume that amount =11, m = 50, n =10 and p =5. Evaluate the following expressions:

a)      n / p + 3

b)      m / p + n – 10 * amount

c)      –p * n

d)     amount / 5

e)      m + n / p + amount

 

  1. Write a function that would be sent in and integer, determines if it is even or odd and prints a corresponding even or odd.

 

 

  1. For the following code, determine the total number of items displayed. Also determine the first and last numbers printed.

int num = 0;

while (num <= 20)

{

    num++;

    cout << num << “ “;

}

 

  1. What is the output of the following loop?

for ( i = 20; i >= 0; i = i -4 )

    cout << i << “ “;

 

  1. Use a loop to display the multiples of 3 backwards from 33 to 3, inclusive.

 

 

  1. The following declarations were used to cerate the prices array:

const int NUMGRADES = 500;

double prices[ NUMGRADES ];

            Write the function header for a function named SortArray() that accepts the prices array as a parameter named intAry and returns no value.

 

  1. Write a small C++ function that takes an integer n and returns the sum of all the integers smaller than n.
  2. What is the error here?

char var1, ptr1;

var1 = ‘x’;

ptr1 = &var1;

 

  1. Assume the definition

char c = ‘s’;

char d = ‘x’;

char e;

char *p1 =  &c;

char  *p2 = &d;

char *p3;

 

Assume further that the address of c is 6940, the address of d is 9772 and the address of e is 2224. Now assume that these commands get executed: (the compilers is really bad!!)

p1 =  &c;

p2 = &d;

p3 = p1;

p1 = p2;

p2 = p3;

p3 = &e;

*p3 = *p1;

*p1 = *p2;

*p2 = *p3;

 

Give the values of the following expressions:

a)      &c

b)      d

c)      e

d)     c

e)      p2

f)       p3

g)      *p1

h)      *p2

i)        *p3

     

  1. Write a function to display the numbers in an array. The function receives the size of the array and a pointer to the array. Display the values in the array by changing the address of a pointer called dispPt.

 

 

  1. We have two arrays, A and B, each containing 10 integers. Write a function that checks if every element of array A is equal to its corresponding element in array B. In other words, the function must check if A[0] is equal to B[0], A[1] is equal to  B[1] and so on. The function must accept only two pointer values and return an integer – zero for equal and not zero for unequal.

 

 

 

  1. Give the big-Oh characterization, in terms of n, of the running time of the following code fragments:
    1. void Ex1 ( int n ) {

      int a;

      for ( int i=0; i<n; i++ )

                  a = i;

      }

    1. void Ex1 ( int n ) {

      int a;

      for ( int i=0; i<n; i+=2 )

                  a = i;

}

    1. void Ex1 ( int n ) {

      int a;

      for ( int i=0; i<n*n i++ )

                  a = i;

}

    1. void Ex1 ( int n ) {

      int a;

      for ( int i=0; i<n; i++ )

                  for ( int j=0; j<=i; j++ )

                  a = i;

}

 

 

 

  1. If the efficiency of the algorithm doIt can be expressed as O(n) =n2, calculate the efficiency of the following program segment:

i = 1

loop ( i < n )

    doIt( … )

    i = i * 2

end loop

 

 

  1. Come up with an algorithm of you choice that could have two solutions. Show the two solutions and report on why you would use one solution instead of the other in your program.